<template>
  <el-card :body-style="{ padding: '0px' }">
    <template v-if="user.googleKey">
      <div id="qrcode" style="margin-left: 50px"></div>
      <div style="padding: 14px;">
        <span>谷歌密钥： {{user.googleKey}}</span>
      </div>
    </template>
    <template v-else>
      <div class="bottom clearfix">
        <el-button type="primary" size="mini" @click="submit">启用谷歌</el-button>
      </div>
    </template>

  </el-card>
</template>

<script>
import QRCode from 'qrcodejs2'
import { resetGoogle } from "@/api/system/user";

export default {
  props: {
    user: {
      type: Object
    }
  },
  components: { QRCode },
  data() {
    return {
    };
  },
  watch: {
    user: {
      handler(user) {
        this.$nextTick(()=> {
          if(user.googleKey) {
            new QRCode(document.getElementById("qrcode"), {
              text: user.googleKey,
              width: 180,
              height: 180,
              colorDark: "#000000",
              colorLight: "#ffffff",
              correctLevel: QRCode.CorrectLevel.H
            })
          }
        })
      },
      immediate: true
    }
  },
  methods: {
    submit() {
      resetGoogle().then(response => {
        this.$props.user = response.data;
        this.$modal.msgSuccess("启用成功");
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>
